home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / DistUpgrade / DistUpgradeMain.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  5.0 KB  |  127 lines

  1. # DistUpgradeMain.py 
  2. #  
  3. #  Copyright (c) 2004-2008 Canonical
  4. #  
  5. #  Author: Michael Vogt <michael.vogt@ubuntu.com>
  6. #  This program is free software; you can redistribute it and/or 
  7. #  modify it under the terms of the GNU General Public License as 
  8. #  published by the Free Software Foundation; either version 2 of the
  9. #  License, or (at your option) any later version.
  10. #  This program is distributed in the hope that it will be useful,
  11. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #  GNU General Public License for more details.
  14. #  You should have received a copy of the GNU General Public License
  15. #  along with this program; if not, write to the Free Software
  16. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. #  USA
  18.  
  19. from DistUpgradeController import DistUpgradeController
  20. from DistUpgradeConfigParser import DistUpgradeConfig
  21. import logging
  22. import os
  23. import sys
  24. import glob
  25. import shutil
  26. from datetime import datetime
  27. from optparse import OptionParser
  28. from gettext import gettext as _
  29.  
  30. def do_commandline():
  31.     " setup option parser and parse the commandline "
  32.     parser = OptionParser()
  33.     parser.add_option("-s", "--sandbox", dest="useAufs", default=False,
  34.                       action="store_true",
  35.                       help=_("Sandbox upgrade using aufs"))
  36.     parser.add_option("-c", "--cdrom", dest="cdromPath", default=None,
  37.                       help=_("Use the given path to search for a cdrom with upgradable packages"))
  38.     parser.add_option("--have-prerequists", dest="havePrerequists",
  39.                       action="store_true", default=False)
  40.     parser.add_option("--with-network", dest="withNetwork",action="store_true")
  41.     parser.add_option("--without-network", dest="withNetwork",action="store_false")
  42.     parser.add_option("--frontend", dest="frontend",default=None,
  43.                       help=_("Use frontend. Currently available: \n"\
  44.                              "DistUpgradeViewText, DistUpgradeViewGtk, DistUpgradeViewKDE"))
  45.     parser.add_option("--mode", dest="mode",default="desktop",
  46.                       help=_("*DEPRECATED* this option will be ignore"))
  47.     parser.add_option("--partial", dest="partial", default=False,
  48.                       action="store_true", 
  49.                       help=_("Perform a partial upgrade only (no sources.list rewriting)"))
  50.     parser.add_option("--datadir", dest="datadir", default=None,
  51.                       help=_("Set datadir"))
  52.     return parser.parse_args()
  53.     
  54. def setup_logging(options, config):
  55.     " setup the logging "
  56.     logdir = config.getWithDefault("Files","LogDir","/var/log/dist-upgrade/")
  57.     if not os.path.exists(logdir):
  58.         os.mkdir(logdir)
  59.     # check if logs exists and move logs into place
  60.     if glob.glob(logdir+"/*.log"):
  61.         now = datetime.now()
  62.         backup_dir = logdir+"/%04i%02i%02i-%02i%02i" % (now.year,now.month,now.day,now.hour,now.minute)
  63.         if not os.path.exists(backup_dir):
  64.             os.mkdir(backup_dir)
  65.         for f in glob.glob(logdir+"/*.log"):
  66.             shutil.move(f, os.path.join(backup_dir,os.path.basename(f)))
  67.     fname = os.path.join(logdir,"main.log")
  68.     # do not overwrite the default main.log
  69.     if options.partial:
  70.         fname += ".partial"
  71.     logging.basicConfig(level=logging.DEBUG,
  72.                         filename=fname,
  73.                         format='%(asctime)s %(levelname)s %(message)s',
  74.                         filemode='w')
  75.     # log what config files are in use here to detect user
  76.     # changes
  77.     logging.info("Using config files '%s'" % config.config_files)
  78.     return logdir
  79.     
  80. def setup_view(options, config, logdir):
  81.     " setup view based on the config and commandline "
  82.  
  83.     # the commandline overwrites the configfile
  84.     for requested_view in [options.frontend]+config.getlist("View","View"):
  85.         if not requested_view:
  86.             continue
  87.         try:
  88.             view_modul = __import__(requested_view)
  89.             view_class = getattr(view_modul, requested_view)
  90.             break
  91.         except (ImportError, AttributeError, TypeError), e:
  92.             logging.warning("can't import view '%s' (%s)" % (requested_view,e))
  93.             print "can't load %s (%s)" % (requested_view, e)
  94.     else:
  95.         logging.error("No view can be imported, aborting")
  96.         print "No view can be imported, aborting"
  97.         sys.exit(1)
  98.     return view_class(logdir=logdir)
  99.  
  100. def main():
  101.     " main method "
  102.     
  103.     # commandline setup and config
  104.     (options, args) = do_commandline()
  105.     config = DistUpgradeConfig(".")
  106.     logdir = setup_logging(options, config)
  107.  
  108.     from DistUpgradeVersion import VERSION
  109.     logging.info("release-upgrader version '%s' started" % VERSION)
  110.  
  111.     # create view and app objects
  112.     view = setup_view(options, config, logdir)
  113.     app = DistUpgradeController(view, options, datadir=options.datadir)
  114.  
  115.     # partial upgrade only
  116.     if options.partial:
  117.         if not app.doPartialUpgrade():
  118.             sys.exit(1)
  119.         sys.exit(0)
  120.  
  121.     # full upgrade
  122.     app.run()
  123.  
  124.